## How do people evaluate foreign aid to “nasty” regimes?
## Tobias Heinrich & Yoshiharu Kobayashi
## British Journal of Political Science
#########################################################

## Auxiliary functions for the analysis

## This expands the surveys and turns them into a format with
## individual evaluations
format_raw_data <- function(x)
{
  rounds <- 1:4
  lr <- c("A", "B") 
  Rating <- B <- C <- PI <- RA <- RT <- rep(NA, length(rounds)*length(lr))
  
  counter <- 1
  for(i in 1:length(rounds))
  {
    for(j in 1:length(lr))
    {
      Rating[counter] <- x[1, paste("Q5", lr[j], rounds[i], sep="_")]
      B[counter] <- x[1, paste("Q5", lr[j], "BENEFIT", rounds[i], sep="_")]
      C[counter] <- x[1, paste("Q5", lr[j], "COSTS", rounds[i], sep="_")]
      PI[counter] <- x[1, paste("Q5", lr[j], "POTISSUE", rounds[i], sep="_")]
      RA[counter] <- x[1, paste("Q5", lr[j], "POTISSUEREMEDYAMOUNT", rounds[i], sep="_")]
      RT[counter] <- x[1, paste("Q5", lr[j], "POTISSUEREMEDYTARGET", rounds[i], sep="_")]
      counter <- counter + 1
    }
  }
  
  tmp <- data.frame(Round=rep(1:4, each=2),
                    LR=rep(c("L", "R"), 4),
                    BirthYr=x$BIRTHYR,
                    Gender=x$GENDER,
                    ZIP=x$INPUTZIP,
                    State=x$STATE,
                    Education=x$Q2_2,
                    Income=x$Q2_3,
                    Q4Time=x$Q4_TIME,
                    Q5Time=x$Q5_TIME,
                    SFail=x$SCREENER_FAILURES_Q3,
                    RepDem=x$Q3_1,
                    Rating=Rating, B=B, C=C, PI=PI, RA=RA, RT=RT,
                    Occupation=x$Q6_1,
                    EconChange=x$Q6_2)
  return(tmp)
}

# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols:   Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#

## From http://wiki.stdout.org/rcookbook/Graphs/Multiple%20graphs%20on%20one%20page%20(ggplot2)/

multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
  require(grid)
  
  # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)
  
  numPlots = length(plots)
  
  # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
    # Make the panel
    # ncol: Number of columns of plots
    # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
                     ncol = cols, nrow = ceiling(numPlots/cols))
  }
  
  if (numPlots==1) {
    print(plots[[1]])
    
  } else {
    # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
    
    # Make each plot, in the correct location
    for (i in 1:numPlots) {
      # Get the i,j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
      
      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
                                      layout.pos.col = matchidx$col))
    }
  }
}


## Rearranging data for design effects tests
############################################
rearrange_data <- function(x)
{
  tmp <- data.frame(ID=x[1, "ID"], Round=x[1, "Round"],
                    Rating_L=x[1, "Rating"], Rating_R=x[2, "Rating"],
                    B_L=x[1, "B"], B_R=x[2, "B"],
                    C_L=x[1, "C"], C_R=x[2, "C"],
                    TC_L=x[1, "TC"], TC_R=x[2, "TC"],
                    PI_L=x[1, "PI"], PI_R=x[2, "PI"],
                    w=x[1, "w"])
  return(tmp)
}

